package com.frxuexi.mdm2205.view

import androidx.compose.foundation.Image
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.res.painterResource
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.frxuexi.mdm2205.MyNavHost
import com.frxuexi.mdm2205.R


sealed class NavItem(val route: String, val title: String, val img: Int) {
    object MessagePage : NavItem("message", "消息", R.drawable.icon1275)
    object HomePage : NavItem("home", "门户", R.drawable.icon64)
    object MinePage : NavItem("mine", "我", R.drawable.icon64)
}
@Composable
fun MainPage() {
    val list = listOf<NavItem>(NavItem.MessagePage, NavItem.HomePage, NavItem.MinePage)
    val navController = rememberNavController()
    val stackEntryState by navController.currentBackStackEntryAsState()
    val destination = stackEntryState?.destination
    Scaffold(
        bottomBar = {
            //解决二级以上页面不显示底部导航
            if(destination?.hierarchy?.any{
                list.map { navItem->navItem.route }.contains(it.route)}==true) {
                NavigationBar {
                    list.forEach { nav ->
                        NavigationBarItem(selected = destination?.hierarchy?.any { it.route == nav.route } == true,
                            onClick = {
                                navController.navigate(nav.route) {
                                    popUpTo(navController.graph.startDestinationId) {
                                        saveState = true//保存状态
                                    }
                                    launchSingleTop = true//栈顶复用
                                    restoreState = true//恢复状态
                                }
                            },
                            icon = {
                                Image(
                                    painter = painterResource(id = nav.img),
                                    contentDescription = ""
                                )
                            },
                            label = { Text(text = nav.title) })
                    }
                }
            }
        }
    ) {
        MyNavHost(it = it, navController = navController)
    }
}